<template>
  <el-form
    ref="ruleFormRef"
    :model="form"
    status-icon
    :rules="rules"
    label-width="100px"
    class="login-container"
  >
    <h3 class="login-title">系统登录</h3>
    <el-form-item
      label="用户名："
      label-width="80px"
      prop="username"
      class="username"
    >
      <el-input 
        type="input" 
        v-model="form.username"
        autocomplete="off"
        placeholder="请输入用户名"
      ></el-input>
    </el-form-item>
    <el-form-item
      label="密码："
      label-width="80px"
      prop="password"
      class="password"
    >
      <el-input
        type="password"
        v-model="form.password"
        autocomplete="off"
        placeholder="请输入密码"
      ></el-input>
    </el-form-item>
    <el-form-item class="login-submit">
      <el-button type="primary" @click="login(ruleFormRef)">登录</el-button>
      <el-button @click="resetForm(ruleFormRef)">重写</el-button>
    </el-form-item>
  </el-form>
</template>
<script setup lang="ts">
import { reactive, toRefs, ref, getCurrentInstance } from 'vue';
import type { FormInstance } from 'element-plus'
import Mock from 'mockjs';
import { useStore } from 'vuex';
import { useRouter } from 'vue-router';


const rules = reactive({
  username: [
    { required: true, message: "请输入用户名", trigger: "blur" },
    { min: 3, message: "用户名长度不能小于3", trigger: "blur" }
  ],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" }
  ]
})
const ruleFormRef = ref<FormInstance>()
const state = reactive({
  form: {
    username: '',
    password: ''
  }
})
const { form } = { ...toRefs(state) };

const router = useRouter();
const store = useStore();
const { proxy } = getCurrentInstance();
const login = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      proxy.axios.post('/permission/getMenu', form.value).then((res) => {
        if (res.status === 200) {
          res = res.data;
          store.commit('CLEARMENU');
          store.commit('SETMENU', res.data.menu);
          store.commit('SETTOKEN', res.data.token);
          store.commit('ADDROUTER', router);
          router.push({ name: 'Index' });
        } else {
          console.log('登录失败');
        }
      })
      // 
      // const token = Mock.Random.guid();
      // store.commit('SETTOKEN', token);
      // router.push({ name: 'Home' });
    } else {
      console.log('error submit!', fields)
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}
</script>
<style lang="less" scoped>
.login-container {
  border-radius: 15px;
  background-clip: padding-box;
  margin: 180px auto;
  width: 350px;
  padding: 35px 35px 15px 35px;
  background-color: #fff;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;

  .login-title {
    margin: 0px auto 40px auto;
    text-align: center;
    color: #505458;
  }
  .login-submit {
    margin: 10px auto 0px auto;
    justify-content: center;
  }
}
</style>